import re

log_path = r'C:/Users/lixiu/Downloads/yijidu/EIMS/eims_nginx_vm.log'

cpu_info_regex = r'^CPU\(s\)\:\s+(?P<cpu_cores>\d+)$'
ssl_regex = r'^\s*listen.*(?P<ssl>ssl).*;'
cpu_usage_regex = r'^\%Cpu\(s\)\: .*, (?P<cpu_idle_percent>[0-9.]*) id, .*$'
nginx_version_regex = r'^nginx version: (?P<nginx_version>[a-zA-Z/]*[0-9.]+)$'
configure_arguments_regex = r'^configure arguments: (?P<nginx_modules>.*)$'
max_open_file_regex = r'^Max open files \W*(?P<max_open_file>[0-9]+).*$'
worker_processes_regex = r'^worker_processes \W*(?P<worker_processes>[0-9]*);$'
worker_connections_regex = r'^\s*worker_connections\s*(?P<worker_connections>\d+);$'
keepalive_timeout_regex = r'^\s*keepalive_timeout\s*(?P<keepalive_timeout>\d+)s;'
client_max_body_size_regex = r'\s*client_max_body_size\s+(?P<client_max_body_size>\d+[a-zA-Z]);'
server_tokens_regex = r'\s*server_tokens\s+(?P<server_tokens>\w+);'
autoindex_regex = r'\s*autoindex\s+(?P<autoindex>\w+);'
memory_regex = r'^Mem:\W+(?P<memory_size>[0-9]+).*$'
with open(log_path, 'r', encoding='utf-8', errors='ignore') as f:
    for line in f:
        line = line.strip()
        if line:
            ssl = re.match(ssl_regex, line)
            cpu_cores = re.match(cpu_info_regex, line)
            nginx_version = re.match(nginx_version_regex, line)
            nginx_modules = re.match(configure_arguments_regex, line)
            max_open_file = re.match(max_open_file_regex, line)
            worker_processes = re.match(worker_processes_regex, line)
            worker_connections = re.match(worker_connections_regex, line)
            keepalive_timeout = re.match(keepalive_timeout_regex, line)
            client_max_body_size = re.match(client_max_body_size_regex, line)
            server_tokens = re.match(server_tokens_regex, line)
            autoindex = re.match(autoindex_regex, line)
            memory = re.match(memory_regex, line)
            if ssl:
                print("ssl", ssl.group('ssl'))
            if cpu_cores:
                print("cpu_cores", cpu_cores.group('cpu_cores'))
            if nginx_version:
                print("nginx_version", nginx_version.group('nginx_version'))
            if nginx_modules:
                print("nginx_modules", nginx_modules.group('nginx_modules'))
            if max_open_file:
                print("max_open_file", max_open_file.group('max_open_file'))
            if worker_processes:
                print("worker_processes", worker_processes.group('worker_processes'))
            if worker_connections:
                print("worker_connections", worker_connections.group('worker_connections'))
            if keepalive_timeout:
                print("keepalive_timeout", keepalive_timeout.group('keepalive_timeout'))
            if client_max_body_size:
                print('client_max_body_size', client_max_body_size.group('client_max_body_size'))
            if server_tokens:
                print('server_tokens', server_tokens.group('server_tokens'))
            if autoindex:
                print('autoindex', autoindex.group('autoindex'))
            if memory:
                print('memory_size', memory.group('memory_size'))
